'''
Created on 19/02/2011

@author: zainul
'''

import Miranda
import gtk

class LogViewerUI:
    
    def __init__(self):
        
        self.builder = gtk.Builder()
        self.builder.add_from_file('./LogWindow.glade')
        self.log_text = self.builder.get_object("log_text")
       
        self.__read_log__()
        self.builder.get_object('refresh_log').connect("clicked", lambda fn: self.__read_log__())
        self.builder.get_object('clear_log').connect("clicked", lambda fn: self.__clear_log__())
        self.builder.get_object('save_log').connect("clicked", lambda fn: self.__save_log__())
       
        self.window = self.builder.get_object("window")       
        self.builder.connect_signals(self)     
       
    
    def __save_log__(self):
        save_dialog = gtk.FileChooserDialog("Open..",
                                   None,
                                   gtk.FILE_CHOOSER_ACTION_SAVE,
                                   (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                    gtk.STOCK_SAVE,gtk.RESPONSE_OK))
        
        response = save_dialog.run()
        
        if (response == gtk.RESPONSE_OK):
            save_log_file = save_dialog.get_filename()
            log_file = open(save_log_file,'w');
            log_file.write(self.log_text.get_label());
            
        save_dialog.destroy()
        
    def __clear_log__(self):
        self.log_text.set_label('')
        
    def __read_log__(self):
        
        log_file = open(Miranda.LOG_FILENAME,'r');
        self.log_text.set_label(log_file.read())
       